﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using imaginecup.util;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Imaginecup.sprite;
using Imaginecup.gameState.intro;
using Imaginecup.gameState.ending.unuse;

namespace Imaginecup.gameState.ending.scene5.unuse
{
    public class EndingScene5_2
    {
        private SimpleDrawObject[] m_ObjectList = new SimpleDrawObject[6];

        private int m_EnableCount = 0;
        private double m_DelayTime = 0.6;
        private double m_CurrentTime = 0;

        private bool m_bStartAnimation;

        public EndingScene5_2(int InitY)
        {
            EntityInfo info = new EntityInfo(55, InitY + 580, 231, 181);

            for( int y = 0 ; y < 2; y ++ )
            {
                info.mPosition.X = 55;

                for( int x = 0 ; x < 3; x++ )
                {
                    int index = y * 3 + x;
                    m_ObjectList[index] = new SimpleDrawObject();
                    m_ObjectList[index].SetEntityInfo(info);

                    info.mPosition.X += info.mSize.X;
                }
                info.mPosition.Y += info.mSize.Y;
            }

            m_EnableCount = 0;
            m_bStartAnimation = false;
        }


        public void LoadEndingScene()
        {
            for (int i = 3; i < 9; i++)
            {
                NormalSprite sprite = new NormalSprite();
                sprite.SetTextureName("" + (i + 1));
                m_ObjectList[i - 3].SetSprite(sprite);
            }
        }

        public void UnloadEndingScene()
        {
            for (int i = 0; i < m_ObjectList.Length; i++)
                m_ObjectList[i].Release();
            m_ObjectList = null;
        }

        public void Update(GameTime gameTime , ScrollMan scrollMan)
        {
            if (!m_bStartAnimation) return;

            m_CurrentTime += gameTime.ElapsedGameTime.TotalSeconds;

            if (m_CurrentTime > m_DelayTime)
            {
                m_CurrentTime = m_CurrentTime - m_DelayTime;
                m_EnableCount++;
                if (m_EnableCount > 6)
                    m_EnableCount = 6;
            }

            for( int i = 0 ; i < m_EnableCount; i++ )
            {
                m_ObjectList[i].SubPositionToInit(new Vector2(0, scrollMan.GetCurrentStartPos().Y));
            }

        }
        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < m_EnableCount; i++)
            {
                m_ObjectList[i].Draw(spriteBatch);
            }
        }

        public void StartAnimation()
        {
            m_bStartAnimation = true;
        }
    }
}
